package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;

import java.util.List;

/**
 * 课程内容管理 Service实现类
 */
public class CourseContentServiceImpl implements CourseContentService {
    CourseContentDao dao = new CourseContentDaoImpl();
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        List<Course_Section> sectionList = dao.findSectionAndLessonByCourseId(courseId);
        return sectionList;
    }

    //根据课程id查询课程信息
    @Override
    public Course findCourseByCourseId(int courseId) {
        Course course = dao.findCourseByCourseId(courseId);
        return course;
    }

    //保存章节信息
    @Override
    public String saveSection(Course_Section section) {

        //1.补全章节信息
        section.setStatus(2); //状态： 0 隐藏；1 待更新；2 已发布
        String dateFormart = DateUtils.getDateFormart();
        section.setCreate_time(dateFormart);
        section.setUpdate_time(dateFormart);

        //2.调用dao
        int i = dao.saveSection(section);

        //3.根据是否插入成功，封装相应信息
        if(i>0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //修改章节信息
    @Override
    public String updateSection(Course_Section section) {
        //1.补全信息
            //获取当前时间
        String date = DateUtils.getDateFormart();
        section.setUpdate_time(date);

        //2.调用dao
        int i = dao.updateSection(section);

        //3.判断是否插入成功
        if(i>0){
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //修改章节状态信息
    @Override
    public String updateSectionStatus(int id, int status) {
        int row = dao.updateSectionStatus(id, status);

        //判断是否修改成功
        if(row>0){
            //修改状态成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            //修改状态失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //添加课时信息
    @Override
    public String saveLesson(Course_Lesson lesson) {

        //补全课时信息
        lesson.setStatus(1);
        String date = DateUtils.getDateFormart();
        lesson.setCreate_time(date);
        lesson.setUpdate_time(date);

        //调用dao
        int row = dao.saveLesson(lesson);

        //判断是否保存成功，封装相信的信息
        if(row>0){
            //添加成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            //添加失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String updateLesson(Course_Lesson lesson) {

        //1.补全信息
        lesson.setUpdate_time(DateUtils.getDateFormart());
        //2.调用dao
        int row = dao.updateLesson(lesson);
        //进行判断是否修改成功
        if (row>0){
            //表示修改成功  返回一个状态信息
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            //表示修改不成功
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }
}
